﻿using AutoMapper;
using DFGK_WMS.Context;
using DFGK_WMS.IRepository;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.Model.Models;
using NPOI.SS.Formula.Functions;
using NPOI.XWPF.UserModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK_WMS.Repository
{
    public class TReceivingGoodsDetailRepository : BaseRepository<TReceivingGoodsDetail>, ITReceivingGoodsDetailRepository
    {
        public TReceivingGoodsDetailRepository(DFGK_DB context, IMapper mapper) : base(context, mapper)
        {
        }

        public bool AddReceivingGoodsDetail(List<TReceivingGoodsDetail> receivingGoodsDetails)
        {
            //使用事务的原子性
            using (var trans = Context.Database.BeginTransaction())
            {
                try
                {
                    if (receivingGoodsDetails.Count > 0)
                    {
                        foreach (var item in receivingGoodsDetails)
                        {
                            Context.TReceivingGoodsDetails.Add(item);
                            Context.SaveChanges();
                        }
                    }
                    Context.SaveChanges();
                    //提交事务
                    trans.Commit();
                    return true;
                }
                catch (Exception)
                {
                    //事务回滚
                    trans.Rollback();
                    throw;
                }
            }


        }

        public List<TReceivingGoodsDetail> GetReceivingGoodsDetailList(int orderDetailid)
        {
            var res = Context.TReceivingGoodsDetails.Where(r => r.OrderDetailId == orderDetailid).ToList();
            if (res.Count()>0)
            {
                return res;
            }
            return null;
        }

        /// <summary>
        /// 采购收货单接口
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public bool AddTPurchaseReceipt(PurchaseReceiptDto dto)
        {

            var receivingBillsDetails = new List<TReceivingBillDetail>();
            var receivingBills = new TReceivingBill
            {
                BillNo = dto.OrderSn,
                //SupplierId = dto.SupplierId,
                SupplierName = dto.SupplierName,
                SupplierCode = dto.SupplierCode,
                Status = 0,
                CreateDate = DateTime.Now,
                BeginDate = DateTime.Now,
                //EndDate = DateTime.Now,
                //采购订单号
                RefSn = dto.PurOrderLineNo,
                TotalQty = dto.ChildList.Sum(x => x.Qty),

            };
            Context.TReceivingBills.Add(receivingBills);
            if (Context.SaveChanges() <= 0)
            {
                return false;
            }


            foreach (var item in dto.ChildList)
            {
                var DetailId = Context.TReceivingBills.Where(x => x.BillNo == dto.OrderSn);
                if(DetailId.Count()<=0)
                {
                    return false;
                }
                int billId = DetailId.First().Id;
                var receivingBill = new TReceivingBillDetail
                {
                    OrderSn = dto.OrderSn,
                    PompanyCode = dto.PompanyCode,
                    PompanyName = dto.PompanyName,
                    BillType = dto.BillType,
                    BillId = billId,
                    MoveType = dto.MoveType,
                    PurOrderLineNo = dto.PurOrderLineNo,
                    DocumentDate = dto.DocumentDate,
                    SupplierCode = dto.SupplierCode,
                    SupplierName = dto.SupplierName,
                    IineItem = item.IineItem,
                    TMaterialCode = item.TMaterialCode,
                    TMaterialName = item.TMaterialName,
                    MaterialCode = item.TMaterialCode,
                    MaterialTypeName = item.TMaterialName,
                    ModelSpec = item.ModelSpec,
                    Qty = item.Qty,
                    ExpectedTime = item.ExpectedTime,
                    CustomerName = item.CustomerName,
                    CustomerCode = item.CustomerCode,
                    FactoryCode = item.FactoryCode,
                    FactoryName = item.FactoryName,
                    FlowPathType = 1
                };

                receivingBillsDetails.Add(receivingBill);
            }

            Context.TReceivingBillDetails.AddRange(receivingBillsDetails);
            if (Context.SaveChanges() > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 生产订单接口
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public bool AddProductionOrder(ProductionOrderDto dto)
        {
            var receivingBills = new TReceivingBillDetail
            {
                OrderSn = dto.OrderSn,
                BillType = dto.BillType,
                PompanyCode = dto.PompanyCode,
                PompanyName = dto.PompanyName,
                TMaterialCode = dto.TMaterialCode,
                TMaterialName = dto.TMaterialName,
                ReceivedQty = dto.ReceivedQty,
                UnitName = dto.UnitName,
                UnitCode = dto.UnitCode,
                PlanStartTime = dto.PlanStartTime,
                PlanEndTime = dto.PlanEndTime,
                FlowPathType = 2
            };

            Context.TReceivingBillDetails.Add(receivingBills);
            if (Context.SaveChanges() > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 生产退料单接口
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public bool AddProductionMaterilas(ProductionMaterilasDto dto)
        {
            var receivingBills = new List<TReceivingBillDetail>();

            foreach (var item in dto.ChildList)
            {
                var receivingBill = new TReceivingBillDetail
                {
                    OrderSn = dto.OrderSn,
                    PompanyCode = dto.PompanyCode,
                    PompanyName = dto.PompanyName,
                    ProductName = dto.ProductName,
                    ProductCode = dto.ProductCode,
                    DocumentDate = dto.DocumentDate,
                    IineItem = item.IineItem,
                    TMaterialCode = item.TMaterialCode,
                    TMaterialName = item.TMaterialName,
                    ModelSpec = item.ModelSpec,
                    Qty = item.Qty,
                    UnitName = item.UnitName,
                    UnitCode = item.UnitCode,
                    FlowPathType = 3
                };

                receivingBills.Add(receivingBill);
            }

            Context.TReceivingBillDetails.AddRange(receivingBills);
            if (Context.SaveChanges() > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 销售订单退货接口
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public bool AddSalesOrderReturn(SalesOrderReturnDto dto)
        {
            var receivingBills = new List<TReceivingBillDetail>();

            foreach (var item in dto.ChildList)
            {
                var receivingBill = new TReceivingBillDetail
                {
                    OrderSn = dto.OrderSn,
                    PompanyCode = dto.PompanyCode,
                    PompanyName = dto.PompanyName,
                    SafeOrderSn = dto.SafeOrderSn,
                    ReturnDateTime = dto.ReturnDateTime,
                    CustomerCode = dto.CustomerCode,
                    CustomerName = dto.CustomerName,
                    IineItem = item.IineItem,
                    SaleIineItem = item.SaleIineItem,
                    TMaterialCode = item.TMaterialCode,
                    TMaterialName = item.TMaterialName,
                    ModelSpec = item.ModelSpec,
                    Qty = item.Qty,
                    UnitName = item.UnitName,
                    UnitCode = item.UnitCode,
                    FlowPathType = 4
                };

                receivingBills.Add(receivingBill);
            }

            Context.TReceivingBillDetails.AddRange(receivingBills);
            if (Context.SaveChanges() > 0)
            {
                return true;
            }
            return false;
        }
    }
}
